﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using YRevitJig;

namespace YArchitech.Plumbing
{
	internal class SprinklerJig : XUiJigBase
	{
		public SprinklerJig(UIDocument uiDoc) : base(uiDoc)
		{
		}

		public bool startPoint(Selection sel, RectLayParamers rect)
		{
			bool result;
			try
			{
				this.rectParas = rect;
				this.rectParas.PointBase = sel.PickPoint("选择起始点坐标，此点为矩形的基点");
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public bool endPoint(Selection sel)
		{
			bool result;
			try
			{
				this.bMove = true;
				this.rectParas.PointDiagonal = sel.PickPoint("选择终止点坐标，此点为矩形的对角点");
				while (this.rectParas.PointBase.IsAlmostEqualTo(this.rectParas.PointDiagonal))
				{
					this.rectParas.PointDiagonal = sel.PickPoint("选择终止点坐标，此点为矩形的对角点");
				}
				RectLayPoint rectLayPoint = new RectLayPoint(this.rectParas, true);
				if (rectLayPoint.Points == null || rectLayPoint.Points.Count < 1)
				{
					result = false;
				}
				else
				{
					this.xyzList = rectLayPoint.Points;
					this.PointEnd = rectLayPoint.PointEnd;
					result = true;
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = false;
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			System.Drawing.Point ptCur = this.m_ptCur;
			if (this.m_ptCur.IsEmpty || paintEventArgs == null)
			{
				return;
			}
			if (this.rectParas == null || !this.bMove)
			{
				return;
			}
			try
			{
				this.rectParas.PointDiagonal = base.Client2Revit(this.m_ptCur);
				if (this.rectParas.PointDiagonal != null)
				{
					RectLayPoint rectLayPoint = new RectLayPoint(this.rectParas, true);
					if (rectLayPoint.Points != null && rectLayPoint.Points.Count >= 1)
					{
						this.xyzList = rectLayPoint.Points;
						this.PointEnd = rectLayPoint.PointEnd;
						XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
						foreach (XYZ point in this.xyzList)
						{
							new XCircle2D(base.Revit2Client(point), this.nRadius).Draw(g);
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}

		private bool bMove;

		private int nRadius = 3;

		public RectLayParamers rectParas;

		public XYZ PointEnd = XYZ.Zero;

		public List<XYZ> xyzList = new List<XYZ>();
	}
}
